//
//  FANPictureCollectionView.swift
//  Weibo - Swift
//
//  Created by user on 16/4/22.
//  Copyright © 2016年 user. All rights reserved.
//

import UIKit

let FANPictureMargin: CGFloat = 5

let FANPictureWH: CGFloat = (ScreenW - 2 * (FANPictureMargin + FANMargin))/3

let collectionViewIdentifier = "collectionViewIdentifier"

class FANPictureCollectionView: UICollectionView,UICollectionViewDataSource,UICollectionViewDelegate {
    
    var dataInfo:(pic_urls:[FANStatusPicInofo], size:CGSize)?{
        
        didSet{
            
            if dataInfo?.pic_urls.count == 1{
                
                let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
                
                layout.itemSize = dataInfo?.size ?? CGSizeZero
                
            }else{
                
                let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
                
                layout.itemSize = CGSize(width: FANPictureWH, height: FANPictureWH)
                
            }
            
            
            self.snp_updateConstraints { (make) -> Void in
                
                make.size.equalTo(dataInfo?.size ?? CGSizeZero)

            }
            
            reloadData()
        }
    }
    
//    var pic_urls:[FANStatusPicInofo]?{
//        
//        didSet{
//            
//            reloadData()
//        }
//    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
        backgroundColor = UIColor.clearColor()
        
        registerClass(FANPictureCollectionCell.self, forCellWithReuseIdentifier: collectionViewIdentifier)
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = CGSizeMake(FANPictureWH, FANPictureWH)
        
        layout.minimumInteritemSpacing = FANPictureMargin
        
        layout.minimumLineSpacing = FANPictureMargin
        
        self.dataSource = self
        
        self.delegate = self;
    }

}

extension FANPictureCollectionView {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataInfo?.pic_urls.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(collectionViewIdentifier, forIndexPath: indexPath) as! FANPictureCollectionCell
        
        cell.photoInfo = dataInfo?.pic_urls[indexPath.item]
        
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        let photoBrowser = SDPhotoBrowser()
        
        photoBrowser.delegate = self;
        
        photoBrowser.currentImageIndex = indexPath.item
        
        photoBrowser.imageCount = dataInfo!.pic_urls.count;
        
        photoBrowser.sourceImagesContainerView = self;
        
        photoBrowser.show()
    }
}

extension FANPictureCollectionView:SDPhotoBrowserDelegate{
    
    func photoBrowser(browser: SDPhotoBrowser!, highQualityImageURLForIndex index: Int) -> NSURL! {
       
        let urlString = dataInfo!.pic_urls[index].thumbnail_pic!.stringByReplacingOccurrencesOfString("thumbnail", withString: "bmiddle")
        
        return NSURL(string: urlString)
        
    }
    
    func photoBrowser(browser: SDPhotoBrowser!, placeholderImageForIndex index: Int) -> UIImage! {
        
        let cell = self.cellForItemAtIndexPath(NSIndexPath(forItem: index, inSection: 0)) as! FANPictureCollectionCell
        
        return cell.iconImageView.image
    }

}

class FANPictureCollectionCell: UICollectionViewCell {
    
    
    var photoInfo:FANStatusPicInofo?{
        
        didSet{
            
            iconImageView.sd_setImageWithURL(NSURL(string: photoInfo!.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_image_placeholder"))
            
            gifImageView.hidden = !((photoInfo?.thumbnail_pic ?? "").hasSuffix(".gif"))
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
        addSubview(iconImageView)
        addSubview(gifImageView)
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            make.size.equalTo(contentView)
        }
        
        gifImageView.snp_makeConstraints { (make) -> Void in
            
            make.bottom.trailing.equalTo(contentView)
        }
    }
    
    
    private lazy var iconImageView:UIImageView = {
    
        let imageView = UIImageView()
        
        imageView.contentMode = .ScaleAspectFill
        
        imageView.clipsToBounds = true
    
        return imageView
    }()
    
    private lazy var gifImageView:UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
    
    
}
